﻿using System;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Xml.Serialization;

namespace iSteam.Sdk.Data
{
    public enum RefreshType
    {
        Data,
        GroupList,
        UserList,
        All
    }

    public enum LoadType
    {
        OnRequest,
        DataOnly,
        All
    }

    public enum Source
    {
        Mobile, 
        Community
    }

    public enum LoginStatus
    {
        LoginFailed,
        LoginSuccessful,
        SteamGuard
    }

    public enum UserStatus
    {
        Offline = 0,
        Online = 1,
        Busy = 2,
        Away = 3,
        Snooze = 4
    }

    public enum ProfileVisibility
    {
        Private = 1,
        Public = 3,
        FriendsOnly = 8
    }

    public enum AvatarSize
    {
        Small,
        Medium,
        Large
    }

    public enum UpdateType
    {
        UserUpdate,
        Message,
        Emote,
        TypingNotification,
        Unknown
    }

    public enum GroupMembership
    {
        Member,
        All
    }

    [DataContract]
    public class Friend
    {
        [DataMember(Name = "steamid")]
        public String SteamId { get; set; }

        [DataMember(Name = "relationship")]
        public String Relationship { get; set; }

        public bool Blocked
        {
            get { return Relationship.Equals("ignored"); }
        }

        [DataMember(Name = "friend_since")]
        public long FriendSinceAsLong { get; set; }

        public DateTime FriendSince
        {
            get { return DataHelper.UnixTimestamp(FriendSinceAsLong); }
        }
    }

    [DataContract]
    public class Friends
    {
        [DataMember(Name = "friends")]
        public List<Friend> Details { get; set; } 
    }

    [DataContract] 
    public class User
    {
        [DataMember(Name = "steamid")]
        public String SteamId { get; set; }

        [DataMember(Name = "communityvisibilitystate")]
        public ProfileVisibility ProfileVisibility { get; set; }

        [DataMember(Name = "profilestate")]
        public int ProfileState { get; set; }

        [DataMember(Name = "personaname")]
        public String Nickname { get; set; }

        [DataMember(Name = "lastlogoff")]
        public long LastLogoffAsLong { get; set; }

        public DateTime LastLogoff
        {
            get { return DataHelper.UnixTimestamp(LastLogoffAsLong); }
        }

        [DataMember(Name = "profileurl")]
        public String ProfileUrl { get; set; }

        [DataMember(Name = "avatarUrl")]
        public String AvatarUrlUnclean { get; set; }

        public String AvatarUrlClean
        {
            get { return AvatarUrlUnclean == null ? "" : AvatarUrlUnclean.Substring(0, AvatarUrlUnclean.Length - 4); }
        }

        [DataMember(Name = "personastate")]
        public UserStatus Status { get; set; }

        [DataMember(Name = "realname")]
        public String RealName { get; set; }

        [DataMember(Name = "primaryclanid")]
        public String PrimaryGroupId { get; set; }

        [DataMember(Name = "timecreated")]
        public long JoinDateAsLong { get; set; }

        public DateTime JoinDate
        {
            get { return DataHelper.UnixTimestamp(JoinDateAsLong); }
        }

        [DataMember(Name = "loccountrycode")]
        public String LocationCountryCode { get; set; }

        [DataMember(Name = "locstatecode")]
        public String LocationStateCode { get; set; }

        [DataMember(Name = "loccityid")]
        public int LocationCityId { get; set; }
    }

    [DataContract]
    public class Users
    {
        [DataMember(Name = "players")]
        public List<User> Details { get; set; }
    }

    [DataContract]
    public class Group
    {
        [DataMember(Name = "steamid")]
        public String SteamId { get; set; }

        [DataMember(Name = "permission")]
        public String Permission { get; set; }

        [DataMember(Name = "relationship")]
        public String Relationship { get; set; }

        public bool Inviteonly
        {
            get { return Permission.Equals("2"); }
        }
    }

    [DataContract]
    public class Groups
    {
        [DataMember(Name = "groups")]
        public List<Group> Details { get; set; }
    }

    [DataContract]
    public class GroupInfo
    {
        [DataMember(Name = "steamid")]
        public String SteamId { get; set; }

        [DataMember(Name = "timecreated")]
        public long CreationDateAsLong { get; set; }

        public DateTime CreationDate
        {
            get { return DataHelper.UnixTimestamp(CreationDateAsLong); }
        }

        [DataMember(Name = "name")]
        public String Name { get; set; }

        [DataMember(Name = "headline")]
        public String Headline { get; set; }

        [DataMember(Name = "summary")]
        public String Summary { get; set; }

        [DataMember(Name = "abbreviation")]
        public String Abbreviation { get; set; }

        [DataMember(Name = "profileurl")]
        public String ProfilePath { get; set; }

        public String ProfileUrl
        {
            get { return "http://steamcommunity.com/groups/" + ProfilePath; }
        }

        [DataMember(Name = "avatar")]
        public String AvatarUrlUnclean { get; set; }

        public String AvatarUrlClean
        {
            get { return AvatarUrlUnclean == null ? "" : AvatarUrlUnclean.Substring(0, AvatarUrlUnclean.Length - 4); }
        }

        [DataMember(Name = "loccountrycode")]
        public String LocationCountryCode { get; set; }

        [DataMember(Name = "locstatecode")]
        public String LocationStateCode { get; set; }

        [DataMember(Name = "loccityid")]
        public int LocationCityId { get; set; }

        [DataMember(Name = "favoriteappid")]
        public int FavoriteAppId { get; set; }

        [DataMember(Name = "users")]
        public int Members { get; set; }

        [DataMember(Name = "usersonline")]
        public int UsersOnline { get; set; }

        [DataMember(Name = "usersinclanchat")]
        public int UsersInChat { get; set; }

        [DataMember(Name = "usersingame")]
        public int UsersInGame { get; set; }

        [DataMember(Name = "ownerid")]
        public String Owner { get; set; }
    }

    [DataContract]
    public class GroupInfos
    {
        [DataMember(Name = "groups")]
        public List<GroupInfo> Details { get; set; }
    }
    
    [DataContract]
    public class ServerInfo
    {
        [DataMember(Name = "servertime")]
        public long ServerTimeAsLong { get; set; }

        public DateTime ServerTime
        {
            get { return DataHelper.UnixTimestamp(ServerTimeAsLong); }
        }

        [DataMember(Name = "servertimestring")]
        public String ServerTimeString { get; set; }
    }

    [DataContract]
    public class NotificationMessage
    {
        [DataMember(Name = "error")]
        public String Message { get; set; }
    }

    [DataContract]
    public class PollMessage : NotificationMessage
    {
        [DataMember(Name = "messagelast")]
        public int MessageLast { get; set; }

        [DataMember(Name = "messages")]
        public List<Update> UpdateMessages { get; set; }
        

    }

    [DataContract]
    public class Update
    {
        [DataMember(Name = "timestamp")]
        public long TimestampAsLong { get; set; }

        public DateTime Timestamp
        {
            get { return DataHelper.UnixTimestamp(TimestampAsLong); }
        }

        [DataMember(Name = "steamid_from")]
        public String Origin { get; set; }

        [DataMember(Name = "type")]
        public String Type { get; set; }

        public UpdateType MessageType
        {
            get
            {
                if (Type.Equals("saytext") || Type.Equals("my_saytext") || Type.Equals("emote"))
                {
                    return Type.Equals("emote") ? UpdateType.Emote : UpdateType.Message;
                }
                if (Type.Equals("typing"))
                {
                    return UpdateType.TypingNotification;
                }
                return Type.Equals("personastate") ? UpdateType.UserUpdate : UpdateType.Unknown;
            }
        }

        [DataMember(Name = "text")]
        public String MessageText { get; set; }

        public String LocalMessage
        {
            get
            {
                return Type.Equals("my_saytext") ? "my_saytext" : null;
            }
        }

        [DataMember(Name = "persona_state")]
        public String Status { get; set; }

        [DataMember(Name = "persona_name")]
        public String Nickname { get; set; }
    }
    
    [DataContract]
    public class LoginToken
    {
        [DataMember(Name = "steamid")]
        public string SteamId { get; set; }

        [DataMember(Name = "umqid")]
        public string Uid { get; set; }

    }

    [DataContract]
    public class AccessToken
    {
        [DataMember(Name = "access_token")]
        public string Token { get; set; }

        [DataMember(Name = "x_errorcode")]
        public string ErrorCode { get; set; }
    }

    #region Xml Data Objects

    [Serializable]
    [XmlRoot("memberList")]
    public class AdditionalGroupInfo
    {
        [XmlArrayItemAttribute(ElementName = "steamID64", IsNullable = false)]
        [XmlArray("members")]
        public List<string> Members { get; set; }
    }

    public class Member
    {
        
    }

    #endregion
}